package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.Permission;
import com.atguigu.result.Result;
import com.atguigu.result.ResultCodeEnum;
import com.atguigu.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @PACKAGE_NAME: com.atguigu.controller
 * @CLASSNAME: PermissionController
 * @AUTHOR: zhangsan
 * @DATE: 2024/5/3 18:11
 * @SINCE 17.0.7
 * @DESCRIPTION: PermissionController
 */
@Slf4j
@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Reference
    private PermissionService permissionService;

    private static final String PAGE_INDEX = "permission/index";

    private static final String PAGE_CREATE = "permission/create";

    private static final String PAGE_EDIT = "permission/edit";

    private static final String PAGE_SUCCESS = "common/successPage";

    private static final String LIST_ACTION = "redirect:/permission";

    @RequestMapping
    public String index(Model model) {
        List<Permission> hierarchicalMenu = permissionService.findAllMenuPermission();
        model.addAttribute("allMenuPermissionList", hierarchicalMenu);
        return PAGE_INDEX;
    }

    @RequestMapping("/create")
    public String toCreatePage(@RequestParam("parentId") Long parentId,
                               @RequestParam("type") Integer type,
                               @RequestParam("parentName") String parentName,
                               Model model) {
        log.debug("parentId = {},type = {},parentName = {}",parentId,type,parentName);
        if (parentId == null || type == null || parentName == null || parentName.isEmpty()) return null;
        Permission permission = new Permission();
        permission.setParentId(parentId);
        permission.setType(type);
        permission.setParentName(parentName);
        model.addAttribute("permission", permission);
        return PAGE_CREATE;
    }

    @RequestMapping("/save")
    public String insert(Permission permission, Model model) {
        Boolean b = permissionService.insertIfEverExisted(permission);
        if (b) {
            model.addAttribute("messagePage", "新增成功");
        } else {
            model.addAttribute("messagePage", "新增失败");
        }
        return PAGE_SUCCESS;
    }

    @RequestMapping("/edit/{id}")
    public String toEditPage(@PathVariable("id") Long id, Model model) {
        Permission permission = permissionService.getById(id);
        model.addAttribute("permission", permission);
        return PAGE_EDIT;
    }

    @RequestMapping("/update")
    public String update(Permission permission, Model model) {
        Boolean b = permissionService.updateIfEverExisted(permission);
        if (b) {
            model.addAttribute("messagePage", "新增成功");
        } else {
            model.addAttribute("messagePage", "新增失败");
        }
        return PAGE_SUCCESS;
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id) {
        Integer delete = permissionService.delete(id);
        return LIST_ACTION;
    }

    @RequestMapping("/existName")
    @ResponseBody
    public Result existName(@RequestParam("name") String name,
                            @RequestParam("type") Integer type) {
        if (StringUtils.isEmpty(name)) {
            return Result.fail("权限名称为空");
        }
        Permission permission = new Permission();
        permission.setName(name.trim());
        permission.setType(type);
        Permission permission1 = permissionService.selectByTypeNameUrlCode(permission);
        if (permission1 != null) {
            return Result.build("权限名称已经存在", ResultCodeEnum.PERMISSION_EXISTED);
        } else {
            return Result.ok("权限名称可用");
        }

    }

    @RequestMapping("/existUrl")
    @ResponseBody
    public Result existUrl(@RequestParam("url") String url,
                           @RequestParam("type") Integer type) {
        if (StringUtils.isEmpty(url)) {
            return Result.fail("菜单url为空");
        }
        Permission permission = new Permission();
        permission.setUrl(url.trim());
        permission.setType(type);
        Permission permission1 = permissionService.selectByTypeNameUrlCode(permission);
        if (permission1 != null) {
            return Result.build("菜单url已经存在", ResultCodeEnum.PERMISSION_EXISTED);
        } else {
            return Result.ok("菜单url可用");
        }
    }

    @RequestMapping("/existCode")
    @ResponseBody
    public Result existCode(@RequestParam("code") String code,
                            @RequestParam("type") Integer type) {
        if (StringUtils.isEmpty(code)) {
            return Result.fail("权限编码为空");
        }
        Permission permission = new Permission();
        permission.setCode(code.trim());
        permission.setType(type);
        Permission permission1 = permissionService.selectByTypeNameUrlCode(permission);
        if (permission1 != null) {
            return Result.build("权限编码已经存在", ResultCodeEnum.PERMISSION_EXISTED);
        } else {
            return Result.ok("权限编码可用");
        }
    }
}
